| 1 | /* |
| 2 | * wiringPi.h: |
| 3 | * Arduino like Wiring library for the Raspberry Pi. |
| 4 | * Copyright (c) 2012-2017 Gordon Henderson |
| 5 | *********************************************************************** |
| 6 | * This file is part of wiringPi: |
| 7 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 8 | * |
| 9 | * wiringPi is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published by |
| 11 | * the Free Software Foundation, either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * wiringPi is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
| 21 | *********************************************************************** |
| 22 | */ |
| 23 | |
| 24 | #ifndef __WIRING_PI_H__ |
| 25 | #define __WIRING_PI_H__ |
| 26 | |
| 27 | // C doesn't have true/false by default and I can never remember which |
| 28 | // way round they are, so ... |
| 29 | // (and yes, I know about stdbool.h but I like capitals for these and I'm old) |
| 30 | |
| 31 | #ifndef TRUE |
| 32 | # define TRUE (1==1) |
| 33 | # define FALSE (!TRUE) |
| 34 | #endif |
| 35 | |
| 36 | // GCC warning suppressor |
| 37 | |
| 38 | #define UNU __attribute__((unused)) |
| 39 | |
| 40 | // Mask for the bottom 64 pins which belong to the Raspberry Pi |
| 41 | // The others are available for the other devices |
| 42 | |
| 43 | #define PI_GPIO_MASK (0xFFFFFFC0) |
| 44 | |
| 45 | // Handy defines |
| 46 | |
| 47 | // wiringPi modes |
| 48 | |
| 49 | #define WPI_MODE_PINS 0 |
| 50 | #define WPI_MODE_GPIO 1 |
| 51 | #define WPI_MODE_GPIO_SYS 2 |
| 52 | #define WPI_MODE_PHYS 3 |
| 53 | #define WPI_MODE_PIFACE 4 |
| 54 | #define WPI_MODE_UNINITIALISED -1 |
| 55 | |
| 56 | // Pin modes |
| 57 | |
| 58 | #define INPUT 0 |
| 59 | #define OUTPUT 1 |
| 60 | #define PWM_OUTPUT 2 |
| 61 | #define GPIO_CLOCK 3 |
| 62 | #define SOFT_PWM_OUTPUT 4 |
| 63 | #define SOFT_TONE_OUTPUT 5 |
| 64 | #define PWM_TONE_OUTPUT 6 |
| 65 | |
3 | 66 | #define LOW 0 |
0 | 67 | #define HIGH 1 |
| 68 | |
| 69 | // Pull up/down/none |
| 70 | |
| 71 | #define PUD_OFF 0 |
| 72 | #define PUD_DOWN 1 |
| 73 | #define PUD_UP 2 |
| 74 | |
| 75 | // PWM |
| 76 | |
| 77 | #define PWM_MODE_MS 0 |
| 78 | #define PWM_MODE_BAL 1 |
| 79 | |
| 80 | // Interrupt levels |
| 81 | |
| 82 | #define INT_EDGE_SETUP 0 |
| 83 | #define INT_EDGE_FALLING 1 |
| 84 | #define INT_EDGE_RISING 2 |
| 85 | #define INT_EDGE_BOTH 3 |
| 86 | |
| 87 | // Pi model types and version numbers |
| 88 | // Intended for the GPIO program Use at your own risk. |
| 89 | |
| 90 | #define PI_MODEL_A 0 |
| 91 | #define PI_MODEL_B 1 |
| 92 | #define PI_MODEL_AP 2 |
| 93 | #define PI_MODEL_BP 3 |
| 94 | #define PI_MODEL_2 4 |
| 95 | #define PI_ALPHA 5 |
| 96 | #define PI_MODEL_CM 6 |
| 97 | #define PI_MODEL_07 7 |
| 98 | #define PI_MODEL_3 8 |
| 99 | #define PI_MODEL_ZERO 9 |
| 100 | #define PI_MODEL_CM3 10 |
| 101 | #define PI_MODEL_ZERO_W 12 |
| 102 | #define PI_MODEL_3P 13 |
| 103 | |
| 104 | #define PI_VERSION_1 0 |
| 105 | #define PI_VERSION_1_1 1 |
| 106 | #define PI_VERSION_1_2 2 |
| 107 | #define PI_VERSION_2 3 |
| 108 | |
| 109 | #define PI_MAKER_SONY 0 |
| 110 | #define PI_MAKER_EGOMAN 1 |
| 111 | #define PI_MAKER_EMBEST 2 |
| 112 | #define PI_MAKER_UNKNOWN 3 |
| 113 | |
| 114 | extern const char *piModelNames [16] ; |
| 115 | extern const char *piRevisionNames [16] ; |
| 116 | extern const char *piMakerNames [16] ; |
| 117 | extern const int piMemorySize [ 8] ; |
| 118 | |
| 119 | |
| 120 | // Intended for the GPIO program Use at your own risk. |
| 121 | |
| 122 | // Threads |
| 123 | |
| 124 | #define PI_THREAD(X) void *X (UNU void *dummy) |
| 125 | |
| 126 | // Failure modes |
| 127 | |
| 128 | #define WPI_FATAL (1==1) |
| 129 | #define WPI_ALMOST (1==2) |
| 130 | |
| 131 | |
| 132 | // wiringPiNodeStruct: |
| 133 | // This describes additional device nodes in the extended wiringPi |
| 134 | // 2.0 scheme of things. |
| 135 | // It's a simple linked list for now, but will hopefully migrate to |
| 136 | // a binary tree for efficiency reasons - but then again, the chances |
| 137 | // of more than 1 or 2 devices being added are fairly slim, so who |
| 138 | // knows.... |
| 139 | |
| 140 | struct wiringPiNodeStruct |
| 141 | { |
| 142 | int pinBase ; |
| 143 | int pinMax ; |
| 144 | |
| 145 | int fd ; // Node specific |
| 146 | unsigned int data0 ; // ditto |
| 147 | unsigned int data1 ; // ditto |
| 148 | unsigned int data2 ; // ditto |
| 149 | unsigned int data3 ; // ditto |
| 150 | |
| 151 | void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ; |
| 152 | void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ; |
| 153 | int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ; |
| 154 | //unsigned int (*digitalRead8) (struct wiringPiNodeStruct *node, int pin) ; |
| 155 | void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; |
| 156 | // void (*digitalWrite8) (struct wiringPiNodeStruct *node, int pin, int value) ; |
| 157 | void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; |
| 158 | int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ; |
| 159 | void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ; |
| 160 | |
| 161 | struct wiringPiNodeStruct *next ; |
| 162 | } ; |
| 163 | |
| 164 | extern struct wiringPiNodeStruct *wiringPiNodes ; |
| 165 | |
| 166 | // Export variables for the hardware pointers |
| 167 | |
| 168 | extern volatile unsigned int *_wiringPiGpio ; |
| 169 | extern volatile unsigned int *_wiringPiPwm ; |
| 170 | extern volatile unsigned int *_wiringPiClk ; |
| 171 | extern volatile unsigned int *_wiringPiPads ; |
| 172 | extern volatile unsigned int *_wiringPiTimer ; |
| 173 | extern volatile unsigned int *_wiringPiTimerIrqRaw ; |
| 174 | |
| 175 | |
| 176 | // Function prototypes |
| 177 | // c++ wrappers thanks to a comment by Nick Lott |
| 178 | // (and others on the Raspberry Pi forums) |
| 179 | |
| 180 | #ifdef __cplusplus |
| 181 | extern "C" { |
| 182 | #endif |
| 183 | |
| 184 | // Data |
| 185 | |
| 186 | // Internal |
| 187 | |
| 188 | extern int wiringPiFailure (int fatal, const char *message, ...) ; |
| 189 | |
| 190 | // Core wiringPi functions |
| 191 | |
| 192 | extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ; |
| 193 | extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ; |
| 194 | |
| 195 | extern void wiringPiVersion (int *major, int *minor) ; |
| 196 | extern int wiringPiSetup (void) ; |
| 197 | extern int wiringPiSetupSys (void) ; |
| 198 | extern int wiringPiSetupGpio (void) ; |
| 199 | extern int wiringPiSetupPhys (void) ; |
| 200 | |
| 201 | extern void pinModeAlt (int pin, int mode) ; |
| 202 | extern void pinMode (int pin, int mode) ; |
| 203 | extern void pullUpDnControl (int pin, int pud) ; |
| 204 | extern int digitalRead (int pin) ; |
| 205 | extern void digitalWrite (int pin, int value) ; |
| 206 | extern unsigned int digitalRead8 (int pin) ; |
| 207 | extern void digitalWrite8 (int pin, int value) ; |
| 208 | extern void pwmWrite (int pin, int value) ; |
| 209 | extern int analogRead (int pin) ; |
| 210 | extern void analogWrite (int pin, int value) ; |
| 211 | |
| 212 | // PiFace specifics |
| 213 | // (Deprecated) |
| 214 | |
| 215 | extern int wiringPiSetupPiFace (void) ; |
| 216 | extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only |
| 217 | |
| 218 | // On-Board Raspberry Pi hardware specific stuff |
| 219 | |
| 220 | extern int piGpioLayout (void) ; |
| 221 | extern int piBoardRev (void) ; // Deprecated |
| 222 | extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; |
| 223 | extern int wpiPinToGpio (int wpiPin) ; |
| 224 | extern int physPinToGpio (int physPin) ; |
| 225 | extern void setPadDrive (int group, int value) ; |
| 226 | extern int getAlt (int pin) ; |
| 227 | extern void pwmToneWrite (int pin, int freq) ; |
| 228 | extern void pwmSetMode (int mode) ; |
| 229 | extern void pwmSetRange (unsigned int range) ; |
| 230 | extern void pwmSetClock (int divisor) ; |
| 231 | extern void gpioClockSet (int pin, int freq) ; |
| 232 | extern unsigned int digitalReadByte (void) ; |
| 233 | extern unsigned int digitalReadByte2 (void) ; |
| 234 | extern void digitalWriteByte (int value) ; |
| 235 | extern void digitalWriteByte2 (int value) ; |
| 236 | |
| 237 | // Interrupts |
| 238 | // (Also Pi hardware specific) |
| 239 | |
| 240 | extern int waitForInterrupt (int pin, int mS) ; |
| 241 | extern int wiringPiISR (int pin, int mode, void (*function)(void)) ; |
| 242 | |
| 243 | // Threads |
| 244 | |
| 245 | extern int piThreadCreate (void *(*fn)(void *)) ; |
| 246 | extern void piLock (int key) ; |
| 247 | extern void piUnlock (int key) ; |
| 248 | |
| 249 | // Schedulling priority |
| 250 | |
| 251 | extern int piHiPri (const int pri) ; |
| 252 | |
| 253 | // Extras from arduino land |
| 254 | |
| 255 | extern void delay (unsigned int howLong) ; |
| 256 | extern void delayMicroseconds (unsigned int howLong) ; |
| 257 | extern unsigned int millis (void) ; |
| 258 | extern unsigned int micros (void) ; |
| 259 | |
| 260 | #ifdef __cplusplus |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #endif |